﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Payroll.Common.Utils;
using Payroll.BusinessLogic.Caching;
using Payroll.BusinessLogic.Data;
using Payroll.Common;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Payroll.BusinessLogic.Company
{
   public partial class MyCompanyService : IMyCompany
    {
       private readonly PayrollHRDataContext m_context;
       public int Save(MyCompany c)
       {
           var item = (from com in m_context.MyCompanies
                       where com.MyCompanyID == c.MyCompanyID
                       select com).FirstOrDefault();

           bool flag = true;
           if (!CommonHelper.IsNull(item))
           {
               flag = false;
           }

           while (flag)
           {
           Search:
               var query = (from co in m_context.MyCompanies
                            where co.MyCompanyID == c.MyCompanyID
                            select co).FirstOrDefault();
               if (CommonHelper.IsNull(query))
               {
                   flag = false;
               }
               else
               {
                 //  this.UpdateCustomerLastId(c.CustomerId);
                   goto Search;
               }
           }
          // c.CustName = c.CustName.ToUpper();

           if (CommonHelper.IsNull(item))
           {
               item = new BusinessLogic.Data.MyCompany
               {

                   #region Assign

                   Name = c.Name,
                   Address=c.Address,
                   EmailSend=c.EmailSend,
                   TaxCode=c.TaxCode,
                   Receive=c.Receive,
                   Createby = c.Createby,
                   Modified = c.Modified,
                   CreateDate = c.CreateDate,
                   ModifiedDate = c.ModifiedDate

                   #endregion
               };
               m_context.MyCompanies.InsertOnSubmit(item);
              // this.UpdateCustomerLastId(c.CustomerId);
           }
           else
           {
               #region CustomerInfo
               item.Name = c.Name;
               item.Address =c.Address;
               item.EmailSend = c.EmailSend;
               item.TaxCode = c.TaxCode;
               item.Receive = c.Receive;
               item.Createby = c.Createby;
               item.Modified = c.Modified;
               item.CreateDate = c.CreateDate;
               item.ModifiedDate = c.ModifiedDate;
               #endregion
           }
           m_context.SubmitChanges();

           return item.MyCompanyID;
       }

    }
}
